package com.yiqi.zhiyuan.feature.main.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.dialog.ActionSheetDialog;
import com.suozhang.framework.widget.dialog.OnOperItemClickL;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.enums.CertificateType;
import com.yiqi.zhiyuan.entity.vo.DiverCenterVO;
import com.yiqi.zhiyuan.feature.mine.contract.FeedbackContract;
import com.yiqi.zhiyuan.feature.mine.dependencies.feedback.DaggerFeedbackComponent;
import com.yiqi.zhiyuan.feature.mine.dependencies.feedback.FeedbackPresenterModule;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.yiqi.zhiyuan.feature.main.view
 * @ClassName: UserInfoActivity
 * @Description: 查看用户信息
 * @Author:
 * @CreateDate: 2019/3/12 14:53
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/12 14:53
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class UserInfoActivity extends BaseActivity implements OnOperItemClickL, FeedbackContract.View {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.img_user_head)
    ImageView mImgUserHead;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;
    @BindView(R.id.tv_user_sex)
    TextView mTvUserSex;
    @BindView(R.id.tv_user_safety_number)
    TextView mTvUserSafetyNumber;
    @BindView(R.id.tv_user_id_number)
    TextView mTvUserIdNumber;
    @BindView(R.id.tv_user_driver_license)
    TextView mTvUserDriverLicense;
    @BindView(R.id.tv_user_license_plate)
    TextView mTvUserLicensePlate;

    private DiverCenterVO mCenterVO;
    ActionSheetDialog dialog;
    private List<LocalMedia> selectList;


    @Inject
    FeedbackContract.Presenter mPresenter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_user_info;
    }


    @Override
    protected void initView() {
        super.initView();


        final String[] stringItems = {"拍照", "从手机相册选择"};
        dialog = new ActionSheetDialog(this, stringItems, null);
        dialog.isTitleShow(false);
        dialog.setOnOperItemClickL(this);


        initToolBar(mToolbar, getString(R.string.user_personal_data), true, true);
    }

    @Override
    protected void initInjector() {
        DaggerFeedbackComponent.builder().feedbackPresenterModule(new FeedbackPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {
        mCenterVO = (DiverCenterVO) this.getIntent().getSerializableExtra("user_info");
        if (mCenterVO != null) {
            setUserData(mCenterVO);
        }
    }

    private void setUserData(DiverCenterVO vo) {
        AM.image().bindToCircleObject(vo.getAvatarUrl(), mImgUserHead);
        mTvUserName.setText(vo.getUsername());
        mTvUserSex.setText(vo.getSex());
        mTvUserSafetyNumber.setText(vo.getDriverNo());
        mTvUserIdNumber.setText(vo.getIsPass()==null?"":CertificateType.getName(vo.getIsPass()));
        mTvUserDriverLicense.setText(vo.getIsPass()==null?"":CertificateType.getName(vo.getIsPass()));
        mTvUserLicensePlate.setText(vo.getCarNo());
    }

    @Override
    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
        dialog.dismiss();
        switch (position) {
            case 0:
                PictureSelector.create(this)
                        .openCamera(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style)
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .enableCrop(true)// 是否裁剪
                        .compress(true)// 是否压缩
                        .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                        .isGif(false)// 是否显示gif图片
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case 1:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style)
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .isCamera(false)
                        .enableCrop(true)// 是否裁剪
                        .compress(true)// 是否压缩
//                        .cropCompressQuality(2)
                        .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                        .isGif(false)// 是否显示gif图片
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .scaleEnabled(true)
                        .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
                break;

        }

        if (dialog != null) {
            dialog.dismiss();
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    for (int i = 0; i < selectList.size(); i++) {
                        String url = selectList.get(i).getCompressPath();
                        mPresenter.updateAvatar(url);
                    }

                    break;
            }
        }
    }

    @OnClick(R.id.btn_upload_avatar)
    public void onViewClicked() {
//        if (dialog != null) {
//            dialog.show();
//        }
    }

    @Override
    public void showUploadImageSuccess(String imageUrl) {
        AM.image().bindToCircleObject(imageUrl, mImgUserHead);
    }

    @Override
    public void showUploadImageError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void showFeedbackSuccess() {

    }

    @Override
    public void showFeedbackError(String errorMsg) {

    }
}
